package edu.suda.xsap.service.impl;

import edu.suda.xsap.dto.MemberDto;
import edu.suda.xsap.dto.convert.MemberConvert;
import edu.suda.xsap.entity.Member;
import edu.suda.xsap.mapper.MemberMapper;
import edu.suda.xsap.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberMapper memberMapper;
@Autowired
private MemberConvert memberConvert;

    @Override
    public void delete(Integer id) {
        memberMapper.delete(id);
    }

    @Override
    public List<MemberDto> findAll() {
        List<Member> members=memberMapper.findAll();
        return memberConvert.toDtoList(members);
    }

    @Override
    public MemberDto findById(Integer id) {
        return memberConvert.toDto(memberMapper.findById(id));
    }

    @Override
    public MemberDto findByName(String name) {
        return memberConvert.toDto(memberMapper.findByName(name));
    }

    @Override
    public void save(Member member) {
        memberMapper.save(member);
    }

    @Override
    public void update(Member member) {
        memberMapper.update(member);
    }

    @Override
    public List<MemberDto> searchByQuery(String query) {
        return memberMapper.findAll().stream()
                .filter(member -> member.getName().contains(query) || member.getPhone().contains(query))
                .map(memberConvert::toDto)
                .collect(Collectors.toList());
    }
    //int getMemberCount();
    @Override
    public int getMemberCount() {
        return memberMapper.findAll().size();
    }

}
